<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\admin\model\Menu as MenuModel;
use app\admin\model\User as UserModel;
use app\admin\model\UserRole as UserRoleModel;
use app\common\controller\Backend;
use think\facade\Db;
use think\facade\Validate;
use think\Request;


class Index extends Backend
{
    public function index() {
        //左侧菜单
        $menu_mod = new MenuModel();
        $left_menu = $menu_mod->where(array('pid'=>0,'style'=>'left','status'=>1))->order('ordid')->select();
        foreach ($left_menu as $key=>$val) {
            $left_menu[$key]['sub'] = $menu_mod->admin_menu($val['id']);
        }
        //顶部菜单
        $top_menu = $menu_mod->where(array('pid'=>0,'style'=>'top','status'=>1))->order('ordid')->select();
        return view('index',[
            'left_menu'=>$left_menu,
            'top_menu'=>$top_menu,
        ]);
    }

    public function login()
    {
        if(request()->isPost()){
            $data = request()->param();
            $errors = [];
            $roleid=UserRoleModel::where(array('isAdmin'=>'1', 'status'=>'1'))->value('id');
            $map['roleid']=array('in',$roleid);
            $map['username'] = $data['username'];
            $map['status']=1;
            $admin = UserModel::where($map)->find();
            if (!$admin) {
                $errors[] = '用户名错误';
                return fail(0,'帐号不存在或已禁用','','add');
            }
            if ($admin['password'] != sha1($data['password'])) {
                $errors[] = '密码错误';
                return fail(0,'密码错误','','add');
            }
            //验证码
            if (!captcha_check($data['verify_code'])) {
                $errors[] = '验证码不正确~';
                return fail(0,'验证码不正确','','add');
            }

            //判断跳转
            if (!empty($errors)) {
                return redirect('/admin/index/login');
            } else {
                session('admin', array(
                    'id' => $admin['uid'],
                    'role_id' => $admin['roleid'],
                    'username' => $admin['username'],
                ));
                return success(1,'登陆成功','','add');

            }
        }else{
            return view('login');
        }

    }


    public function logout()
    {
        session('admin', null);
        return success(1,'退出成功');
    }



    public function panel() {
        $message = array();
        if(env('app_debug')){
            $message[] = array(
                'type' => 'error',
                'content' => "您网站的 DEBUG 没有关闭，出于安全考虑，我们建议您关闭程序 DEBUG。",
                'divid' => 'debug',
            );
        }

        if (is_dir(app()->getBasePath().'/install')) {
            $message[] = array(
                'type' => 'error',
                'content' => "您还没有删除 install 文件夹，出于安全的考虑，我们建议您删除 install 文件夹。",
                'divid' => 'dinstall',
            );
        }

        if (!function_exists("curl_getinfo")) {
            $message[] = array(
                'type' => 'error',
                'content' => "系统不支持 CURL。",
                'divid' => 'dcurl',
            );
        }
        $data['message']=$message;


        $system_info = array(
            'wkcms_version' =>  '<a href="http://www.boyanglee.com/" class="blue" target="_blank">查看最新版本</a>',
            'server_domain' => $_SERVER['SERVER_NAME'] . ' [ ' . gethostbyname($_SERVER['SERVER_NAME']) . ' ]',
            'server_os' => PHP_OS,
            'web_server' => $_SERVER["SERVER_SOFTWARE"],
            'php_version' => PHP_VERSION,
            'mysql_version' => Db::query("select version() as v;")[0]['v'],
            'upload_max_filesize' => ini_get('upload_max_filesize'),
            'max_execution_time' => ini_get('max_execution_time') . '秒',
            'safe_mode' => (boolean) ini_get('safe_mode') ?  '是' : '否',
            'zlib' => function_exists('gzclose') ?   '是' : '否',
            'curl' => function_exists("curl_getinfo") ?  '是' : '否',
            'timezone' => function_exists("date_default_timezone_get") ? date_default_timezone_get() : L('no')
        );

        $data['system_info']=$system_info;
        return view('panel',$data);
    }

}
